// ReadAVIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ReadAVI.h"
#include "ReadAVIDlg.h"
#include <VFW.H>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadAVIDlg dialog

CReadAVIDlg::CReadAVIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReadAVIDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReadAVIDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReadAVIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReadAVIDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReadAVIDlg, CDialog)
	//{{AFX_MSG_MAP(CReadAVIDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_READAVI, OnReadavi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadAVIDlg message handlers

BOOL CReadAVIDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReadAVIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CReadAVIDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
			CRect dibrect;
	CRect destrect;
	if(!(img.IsEmpty()))
	{
		CPaintDC dc(this);
		dibrect.top=0;
		dibrect.left=0;
		dibrect.right=img.GetWidth();
		dibrect.bottom=img.GetHeight();
		destrect=dibrect;
		img.Draw(dc,&destrect,&dibrect);
	}
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CReadAVIDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CReadAVIDlg::OnReadavi() 
{
	// TODO: Add your control notification handler code here

	CString m_cstrFileName;

	CFileDialog dlg( TRUE,_T("AVI"),_T("*.AVI"),
					 OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
					 _T("Animation (*.AVI)|*.AVI|"));

	if( dlg.DoModal()==IDOK )
	{
		m_cstrFileName = dlg.GetPathName();
	}
	else
		return;
	AVIFileInit();
	PAVIFILE avi;
	int res=AVIFileOpen(&avi, m_cstrFileName, OF_READ, NULL);
	int n = GetLastError();
	if (res!=AVIERR_OK)
	{
	//an error occures
		if (avi!=NULL)
		{
			AVIFileRelease(avi);
			return ;
		}
	}
	AVIFILEINFO avi_info;
	AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO));
	PAVISTREAM pStream;
	res=AVIFileGetStream(avi, &pStream, streamtypeVIDEO /*video stream*/, 0 /*first stream*/);
	if (res!=AVIERR_OK)
	{
		if (pStream!=NULL)
		{
	
			AVIStreamRelease(pStream);
			AVIFileExit();
			return ;
		}
	}
	//
	int iNumFrames;
	int iFirstFrame;
	iFirstFrame=AVIStreamStart(pStream);
	if (iFirstFrame==-1)
	{
	//Error getteing the frame inside the stream
		if (pStream!=NULL)
		{
	
			AVIStreamRelease(pStream);
			AVIFileExit();
			return ;
		}
	}
	iNumFrames=AVIStreamLength(pStream);
	if (iNumFrames==-1)
	{
	//Error getteing the number of frames inside the stream
		if (pStream!=NULL)
		{
			AVIStreamRelease(pStream);
			AVIFileExit();
			return ;
		}
	}
	//
	PGETFRAME pFrame;
	pFrame=AVIStreamGetFrameOpen(pStream, NULL );

	AVISTREAMINFO streaminfo;
	AVIStreamInfo(pStream,&streaminfo,sizeof(AVISTREAMINFO));

	//
	BITMAPINFOHEADER bih2;
	long lsize = sizeof(bih2);
	int index=0;
	BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index); //
	AVIStreamReadFormat(pStream,index,&bih2,&lsize);
	//

	BYTE* Bits=new BYTE[bih2.biSizeImage];

	int width=avi_info.dwWidth;
	int height=avi_info.dwHeight;

	img.m_lpBih= (BITMAPINFOHEADER *)new char[sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)];  
	LPBITMAPINFO m_pBMI=(BITMAPINFO *)img.m_lpBih;
	m_pBMI->bmiHeader.biBitCount=8;
	m_pBMI->bmiHeader.biClrImportant=0;
	m_pBMI->bmiHeader.biClrUsed=0;
	m_pBMI->bmiHeader.biCompression=0;
	m_pBMI->bmiHeader.biHeight=height;
	m_pBMI->bmiHeader.biWidth=width;
	m_pBMI->bmiHeader.biPlanes=1;
	m_pBMI->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
	m_pBMI->bmiHeader.biXPelsPerMeter=0;
	m_pBMI->bmiHeader.biYPelsPerMeter=0;
	m_pBMI->bmiHeader.biSizeImage=height*width;
	for (int i=0;i<256;i++) 
	{
	      m_pBMI->bmiColors[i].rgbBlue = i ;
	      m_pBMI->bmiColors[i].rgbGreen = i ;
	      m_pBMI->bmiColors[i].rgbRed = i ;
	      m_pBMI->bmiColors[i].rgbReserved = 0 ;
	}

	img.m_lpImage=new BYTE[width*height];
	for(index=0;index<iNumFrames;index++)
	{
		AVIStreamRead(pStream,index,1,Bits,bih2.biSizeImage,NULL,NULL);
		for(int i=0;i<width*height*4;i+=4)
		{
			img.m_lpImage[i/4]=Bits[i];
		}
		Sleep(20);
		Invalidate(FALSE);
		MSG msg;
		PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
	    DispatchMessage(&msg);
	}
	

	


}
